package com.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Developer;
import com.common.entity.User;
import com.common.vo.PageResult;
import com.web.mapper.DeveloperMapper;
import com.web.mapper.UserMapper;
import com.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpL extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeveloperMapper developerMapper;
    private PageResult pageResult=null;
    public PageResult getUser(String email, String twitter_username, String company, int page, int size) {

        System.out.println(email != null && !email.isEmpty() );
        System.out.println(company != null && !company.isEmpty());
        System.out.println(twitter_username != null && !twitter_username.isEmpty() );
        //如果查询参数为空，则直接全查询返回
        if ( isNotEmpty(email)&&isNotEmpty(twitter_username)&&isNotEmpty(company)){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            IPage<User> pageInfo = new Page<>(page, size);
            IPage<User> userIPage = userMapper.selectPage(pageInfo, queryWrapper);
            pageResult=new PageResult(userIPage.getTotal(),userIPage.getRecords());
            return  pageResult;
        }
        //有email直接查询
        //因为email是唯一的所以查询只有一条
        if (email != null && !email.isEmpty()) {
            List<User> userByEmail = getUserByEmail(email);

            pageResult=new PageResult(1,userByEmail);
//            return getUserByEmail(email);
            return pageResult;
        }
        //没有email，则根据name和company查询出在developer表中的email，
        // 在User表中查询出该email对应的user
        QueryWrapper<Developer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("twitter_username", twitter_username).or(de -> de.eq("company", company));
        Developer developer = developerMapper.selectOne(queryWrapper);
        //获取email
        if(developer!=null) {
            String email1 = developer.getEmail();
            if (isNotEmpty(email1)) {
                List<User> userByEmail = getUserByEmail(email1);

                pageResult = new PageResult(1, userByEmail);
                return pageResult;
            }
        }
        return pageResult;
    }

    //直接通过email查询User
    public List<User> getUserByEmail(String email) {
        User userList = userMapper.selectOne(new QueryWrapper<User>().eq("email", email));
        if(userList!=null){
            return Collections.singletonList(userList);
        }else {
            return Collections.emptyList();
        }
    }


    public static boolean isNotEmpty(String str) {
        return str != null && !str.isEmpty();
    }
}



